<?php
/**
 * DMhezuAction.class.php
 *合租房数据挖掘算法类
 *@author zz
 *@version 1
 *@since 2015-7-20
 */
header("Content-type: text/html; charset=utf-8");
ini_set("memory_limit","4000M");
ini_set('max_execution_time', '0');
include_once('upyun.class.php');
Class DMHezuAction
{


    protected $upyun;

    protected $cityarea_item=array();

    protected $borough=array();

    protected $borough_byname=array();

    protected $house_hezu_gov=array();

    protected $house_hezu=array();

    protected $house_hezu_info=array();

    protected $house_hezu_pic=array();

    protected $house_hezu_del=array();

    protected $broker_check=array();

    protected $auto=array();

    Public function index(){
        echo "this is DMHezu";
    }
    /*
     * 参数初始化
     */
    protected function __set_params($params=''){
        $this->cityarea_item = M('city_item');
        $this->borough = M('borough');
        $this->borough_byname = M('borough_byname');
        $this->house_hezu_gov = M('house_hezu_gov');
        $this->house_hezu = M('house_hezu');
        $this->house_hezu_info = M('house_hezu_info');
        $this->house_hezu_pic = M('house_hezu_pic');
        $this->house_hezu_del = M('house_hezu_del');
        $this->broker_check = M('broker_check');
        $this->auto=M('auto_increment_id');
    }
	/*
    * 分量排除图片上传upanyun失败问题
    */
	public function FixUpanyunPic(){

		$upyun = new UpYun('zhaijiso-pic', 'kys', 'zjsZJS123');
		$house_pic = M('house_hezu_pic');
		$startnum = empty($_GET['startnum'])?1:$_GET['startnum'];
		$endnum = empty($_GET['endnum'])?2000:$_GET['endnum'];
		$where = array('time' => array('lt','FROM_UNIXTIME(1456761600)'));

		$result =$house_pic->where($where)->order('id')->limit($startnum.','.$endnum)->select();
		//echo M()->getLastSql();die;
		foreach($result as $value){
			$pic_info = array();
			$content = file_get_contents($value['pic_url']);
			//错误图片包含错误标签
			if(preg_match('/<head>/u',$content) || $content == false){
				Log::write('pic表中【id】为：'.$value['id'].' 【URL】 为：'.$value['pic_url'].'的需要处理。');
				$time = microtime(true);
				$ypy_path = '/hezu/unit/'.$time.'.jpg';
				try{
					$upyun->writeFile($ypy_path, $content, true);   // 上传图片，自动创建目录
					$pic_info['time'] = date('Y-m-d H:i:s');
				}catch(Exception $e){
					usleep(50000);
					$upyun->writeFile($ypy_path, $content, true);   // 上传图片，自动创建目录
					$pic_info['time'] = date('Y-m-d H:i:s');
				}
				$pic_path = 'http://zhaijiso-pic.b0.upaiyun.com'.$ypy_path;
				Log::write('pic表中【id】为：'.$value['id'].' 【URL】 为：'.$value['pic_url'].'的处理【成功】。');
				$pic_info['pic_url'] = $pic_path;
				$house_pic->where(array('id'=>$value['id']))->save($pic_info);
			}else{
				$pic_info['time'] = date('Y-m-d H:i:s');
				$house_pic->where(array('id'=>$value['id']))->save($pic_info);
				Log::write('pic表中【id】为：'.$value['id'].' 【URL】 为：'.$value['pic_url'].'的【不】需要处理。');
			}
		}
	}
	/*@kevin
	 *@2016-12-18
	 *图片更新时才会调用
	 *
	 */
	public  function handlePic($pic,$id,$gov_id){
	    $this->upyun = new UpYun('zhaijiso-pic', 'kys', 'zjsZJS123');
	    //房源图片表
	    $house_hezu_pic = M('house_hezu_pic');
        //选出所有gov_id对应的图片并去重
        $Pics = $house_hezu_pic->field("pic_source_url")->where('gov_id='.$gov_id)->select();
        $pics = [];
        foreach($Pics as $picc) {
            $pics[] = $picc["pic_source_url"];
        }
	    $picList = array_unique(explode('|',$pic));
	    $pic_info = array();
	    foreach($picList as $k=>$value){
	        if(!empty($value)){
                //如果抓取到的照片链接已存在数据库中则跳过
                if(in_array($value,array_unique($pics))){continue;}
	            $fhunit = file_get_contents($value);
	            $time = microtime(true)*10000;
	            $ypy_path = '/hezu/unit/'.$time.'.jpg';
	            $rsp1 = $this->upyun->writeFile($ypy_path, $fhunit, true); // 上传图片，自动创建目录
	            $endTime = microtime(true);
	            $pic_path = 'http://zhaijiso-pic.b0.upaiyun.com'.$ypy_path;
	            $pic_info[]=array(
	                'pic_source_url'=>$value,
	                'pic_url'=>$pic_path,
	                'house_hezu_id'=>$id['id'],
	                'gov_id'=>$gov_id,
	                'pic_type'=>1,
	                'time'=>date('Y-m-d h:i:s',time())
	            );
	        }else{
	            continue;
	        }
	    }
	    $house_hezu_pic->addAll($pic_info);
	}
	public function houseCount($house_id){

		// 出售房源主表
		$model_hezu = M ( 'house_hezu' );
		// 出售房源info表
		$model_info = M ( 'house_hezu_info' );
		$model_info_delete = M ( 'house_hezu_info_delete' );
		$house_id=trim($house_id);
		$where=array('id'=>$house_id);
		$house_list = $model_hezu->where($where)->select();
		$count = 0;
		if(!empty($house_list[0])){
			foreach ( $house_list as $key => $value ) {
				// 最近时间
				$last_time = 0;
				// 最低价格
				$min_price = 100000000000;
				$house_info_result = array ();
				$arr_del_id_gov=array();
				// 查询house_hezu_info表
				$where = array ();
				$where = array (
						'id' => $value ['id']
				);
				$house_info_list = $model_info->where ( $where )->order ( 'source' )->field ( 'id,source,updated,house_price' )->select ();
		
				// 查询hezu_info表中的数据,查找出每个中介公司中最新的房源
				foreach ( $house_info_list as $k => $v ) {
					if ($house_info_result [$v ['source']] ['updated'] < $v ['updated']) {
						$house_info_result [$v ['source']] = $v;
					}else{
	                    $arr_del_id_gov[]=$v;
	                }
				}
				// 从筛选出来的每个中介公司最新的一条房源中找到整个房源的最低价格和最近更新时间
				foreach ( $house_info_result as $k1 => $v1 ) {
					if($last_time<$v1['updated']){
						$last_time = $v1 ['updated'];
					}
					if ($min_price > $v1 ['house_price']) {
						$min_price = $v1 ['house_price'];
					}
				}
				// 					echo "最低价".$min_price;
				// 					echo "最近时间".$last_time;
				// 删除house_hezu_info中数据(id= 并且 sourcre= 并且 updated！= )
				$num_fangzhu=0;
				$is_fangzhu = 0;
				foreach ( $house_info_result as $k2 => $v2 ) {
					if($v2['source'] == 10){
						$num_fangzhu=1;
						$is_fangzhu = 1;
					}
					/*
					if (! empty ( $arr_del_id_gov )) {
						$model_info_delete->addAll ( $arr_del_id_gov );
						$model_info->where ( $arr_del_id_gov )->delete ();
					}
					*/
				}
				// 增加house_hezu 2字段值
				
				if (($value['min_price'] == $min_price) && ($value['updated'] == $last_time) && ($value['is_fangzhu'] == $is_fangzhu) && ($value['cnt'] == count($house_info_result) + $num_fangzhu)) {
				    continue;
				}elseif($min_price!=100000000000 && $last_time!=0){
					$value ['min_price'] = $min_price;
					$value ['updated'] = $last_time;
					$value ['is_fangzhu'] = $is_fangzhu;
					$value['cnt'] = count($house_info_result)+$num_fangzhu;
					$model_hezu->save ( $value );
					Log::write("【更新hezu中cnt,minprice,updated】" . $model_hezu->getLastSql(), 'INFO');
				}
				// 					echo M()->getLastSql();
				// 					echo "success";die;
			}
		}
		// 			die;
		
		
		
	}
	Public function fetch(){
        $city = isset($_GET["city"])?"beijing":$_GET["city"];
	    //初始化参数
        $this->__set_params();
        $this->upyun = new UpYun('zhaijiso-pic', 'kys', 'zjsZJS123');
	    $list=array();
	    //查GOV表未对比的数据  并且模糊对比成功 is_contrast(1、精确对比成功。2、未对比。3、模糊对比成功 4、精确对比失败  5、模糊对比失败 6、精确无对比  7、模糊无对比
		$list=$this->house_hezu_gov->where('is_contrast = 2 and is_fill=2 and source != 10  ')->limit("0,100")->select();

        while(!empty($list)){
            foreach($list as $key=>$value){
                $value['created'] = microtime(true);
                $value['updated'] = microtime(true);

                $value["house_floor"] = trimall($value["house_floor"]);
                //处理楼层
                $this->handleFloor($value);

                //$v    info 表中的数据
                //$value  gov 表中的数据
                if($this->result){
                    $arr_bath_insert = $this->countWeight($this->result,$value);
                }
                //连接redis
                $this->connect_redis();
                //判断是否有比对数据
                if(!empty($arr_bath_insert)){
                    //有对比数据
                    $this->moreHouse($arr_bath_insert,$value);
                    //对比上的房源数beijing_2016_04_21_hezu_$contrast_cnt
                    $date = date('Y-m-d',time());
                    $contrast_cnt = count($arr_bath_insert);
                    $redis_key = $city."_".$date."_hezu_more_cnt";
                    if($this->redis->exists($redis_key)){
                        $this->redis->incrBy($redis_key, $contrast_cnt);
                    }else{
                        $this->redis->set($redis_key,$contrast_cnt);
                    }
                    unset($arr_bath_insert);
                }else{
                    //无对比数据
                    //独立房源数beijing_2016_04_21_hezu_alone_cnt
                    $date = date('Y-m-d',time());
                    $contrast_cnt = count($this->result);
                    $redis_key = $city."_".$date."_hezu_alone_cnt";
                    if($this->redis->exists($redis_key)){
                        $this->redis->incrBy($redis_key, $contrast_cnt);
                    }else{
                        $this->redis->set($redis_key,$contrast_cnt);
                    }
                    $this->aloneHouse($this->result,$value);

                }
            }
        $list=$this->house_hezu_gov->where('is_contrast = 2 and is_fill=2 and source !=10')->limit("0,100")->select();
        }
    }
    /*
     * 处理楼层，$value["house_floor"]
     */
	public function handleFloor($value){

        $is_num = is_numeric($value["house_floor"]);
        $this->result=[];
        //楼层处理之后应该独立出来优化
        if($is_num){
            $house_floor = $value['house_floor'];
            if($value['house_floor']<($value['house_topfloor']/3)){
                $value['house_floor'] = "低";
            }elseif($value['house_floor']>=($value['house_topfloor']/3) && $value['house_floor']<=($value['house_topfloor']*(2/3))){
                $value['house_floor'] = "中";
            }else{
                $value['house_floor'] = "高";
            }
            Log::write("【所在楼层是数字 已转换为 汉字】".$value['house_floor'],'INFO');
            //用其他经纪公司的数据查 house_hezu_info 表
            $this->result = $this->house_hezu->table(array('house_hezu'=>'a','house_hezu_info'=>'b'))
                ->field('a.*,b.*')
                ->where('a.id=b.id and b.house_topfloor='.$value['house_topfloor'].
                    ' and( b.house_floor = '.$house_floor.
                    ' or b.house_floor like "%'.$value['house_floor'].'%") and a.borough_id = '.$value['borough_id'].
                    ' and a.house_room = '.$value['house_room'].
                    ' and (
                                                    (a.house_room_totalarea >= '.$value['house_room_totalarea'].'-1'.
                    ' and a.house_room_totalarea <='.$value['house_room_totalarea'].'+1
                                                    )
                                                    or a.house_room_totalarea ='.$value['house_room_totalarea'].'
                                            ) and b.source != '.$value['source']
                )
                ->group('b.id')
                ->select();
            Log::write("【用gov表数据做条件查 house_hezu_info 表】".$this->house_hezu->getLastSql(),'INFO');
            //echo "<br/>";
        }else{
            preg_match("/(中|高|低).*/",$value['house_floor'], $floor);
            $value['house_floor'] = $floor[1];
            //如果是汉字换算成数字
            if ($value['house_floor'] == '低'){
                $min = -1;
                $max = ($value['house_topfloor']/3);
            }elseif ($value['house_floor'] == '中'){
                $min = ($value['house_topfloor']/3);
                $max = ($value['house_topfloor']*(2/3));
            }elseif($value['house_floor'] == '高'){
                $min = ($value['house_topfloor']*(2/3));
                $max = $value['house_topfloor'];
            }else{
                $min = -999;
                $max = -99;
            }
            Log::write("【所在楼层是汉字 已转换为 数字区间】".$min."至".$max,'INFO');
            //用其他经纪公司的数据查 house_hezu_info 表
            $this->result = $this->house_hezu->table(array('house_hezu'=>'a','house_hezu_info'=>'b'))
                ->field('a.*,b.*')
                ->where('a.id=b.id and b.house_topfloor='.$value['house_topfloor'].
                    ' and( b.house_floor like "%'.$value['house_floor'].
                    '%" or (b.house_floor>='.$min.' and b.house_floor<='.$max.')) and a.borough_id = '.$value['borough_id'].
                    ' and a.house_room = '.$value['house_room'].
                    ' and (
                                                    (a.house_room_totalarea >= '.$value['house_room_totalarea'].'-1'.
                    ' and a.house_room_totalarea <='.$value['house_room_totalarea'].'+1
                                                    )
                                                    or a.house_room_totalarea ='.$value['house_room_totalarea'].'
                                            ) and b.source != '.$value['source']
                )
                ->group('b.id')
                ->select();
            log::write("【用gov表数据做条件查 house_hezu_info 表】".$this->house_hezu->getLastSql(),'INFO');
        }
    }
    /*
     * 计算权重
     */
    public function countWeight($result,$value){
        $arr_bath_insert = [];
        foreach($result as $k=>$v){
            $weight=0;
            //最大权重的房源
            $value_temp=array();
            $v_temp=array();
            Log::write("house_hezu_info房源ID【".$v['id']."】与 其他经纪公司房源ID【".$value['id']."】对比开始",'INFO');
            //对比厅
            if($v['house_hall'] == $value['house_hall']){
                $weight +='31.00';
                Log::write("对比厅后，权重：【".$weight."】",'INFO');
            }
            //对比房屋价格 上下浮动5%
            if($value['house_price']*0.95<=$v['house_price'] &&  $v['house_price']<= $value['house_price']*1.05){
                $weight_price=(1-abs($value['house_price']-$v['house_price'])/$value['house_price']/0.05)*25.00;
                $weight_price=$weight_price==0?1:$weight_price;
                //容错
                $weight_price=$weight_price>25?25:$weight_price;
                $weight +=$weight_price;
                Log::write("对比价格后，权重：【".$weight."】",'INFO');
            }else{
                $weight_price=(1-abs($value['house_price']-$v['house_price'])/$value['house_price']/0.05)*60.00;
                $weight_price=$weight_price==0?1:$weight_price;
                $weight_price=$weight_price<-60?-60:$weight_price;
                $weight +=$weight_price;
                Log::write("对比价格后，权重：【".$weight."】",'INFO');
            }
            //对比装修情况
            if($v['house_fitment'] == $value['house_fitment']){
                $weight +='25.00';
                Log::write("对比装修后，权重：【".$weight."】",'INFO');
            }
            //对比朝向
            if(self::tVt($v['house_toward'],$value['house_toward'])){
                $weight +='12.00';
                Log::write("对比朝向后，权重：【".$weight."】",'INFO');
            }
            //对比卫生间
            if($v['house_toilet'] == $value['house_toilet']){
                $weight +='7.00';
                Log::write("对比卫生间后，权重：【".$weight."】",'INFO');
            }
            if($weight >='35.00'){
                /**
                 * $value_temp 是 info 表中的数据
                 * $v_temp 是 gov表中的数据
                 */
                $value_temp = $value;
                $v_temp = $v;
                Log::write("总权重值:【".$weight."】",'INFO');

                $value_temp['gov_id'] = $value_temp['id'];
                $value_temp['id'] = $v_temp['id'];
                $value_temp['weight'] = $weight;
                $arr_bath_insert[]=$value_temp;
            }
        }
        return $arr_bath_insert;
    }
    /*
     * 连接redis
     */
    protected function connect_redis(){
        /*******连接redis******/
        $this->redis = new Redis();
        $this->redis->connect(C('redis_server'), C('redis_port'));
        $this->redis->auth(C('redis_auth'));
        $this->redis->select(C('redis_db'));
        /*******连接redis******/
    }
    /*
     * 有对比数据入库
     */
    public function moreHouse($arr_bath_insert,$value){

        //权重 大于 35 把is_contrast = 3 模糊对比成功
        $data=array();
        $data['is_contrast'] = 3;
        $this->house_hezu_gov->where('id='.$value['id'])->save($data);
        Log::$format = '[ Y-m-d H:i:s ]';
        Log::write("【有对比数据】【gov_id为：】".$value['id'],'INFO');
        Log::write("【将gov表中的is_contrast改为3】【gov_id为：】".$value['id'],'INFO');
        //有对比数据
        //计算house_hezu表中的新增字段
        foreach($arr_bath_insert as $str_insert){
            if(!empty($house_floor)){
                $str_insert['house_floor'] = $house_floor;
            }
            $is_contrast=array();
            $this->house_hezu_info->data($str_insert)->add();
            //更改gov表中的字段值is_contrast不等于1的设置为1
            $where_contrast=array('id'=>$str_insert['gov_id']);
            $is_contrast=$this->house_hezu_gov->where($where_contrast)->field('is_contrast')->find();
            if($is_contrast!=1){
                Log::$format = '[ Y-m-d H:i:s ]';
                Log::write("【对比成功的数据之前未对比成功，更改is_contrast为1】【gov_id为：】".$value['id'],'INFO');
                $this->house_hezu_gov->where($where_contrast)->save(array('is_contrast'=>1));
            }
            //计算hezu表中的cnt、min_price
            $this->houseCount($str_insert['id']);
            Log::$format = '[ Y-m-d H:i:s ]';
            Log::write("【计算house_hezu表中的三个字段】【house_hezu的id为：】".$str_insert['id'],'INFO');
        }
        $value = $arr_bath_insert;
        //插入图片
        //插入到house_hezu_pic表
        foreach($value as $kk=>$vv){
            //室内图
            $pic1 = array();
            $pic1 = explode('|',$vv['house_pic_unit']);
            $vv['house_pic_unit'] = $pic1;
            //户型图
            $pic2 = array();
            $pic2 = explode('|',$vv['house_pic_layout']);
            $vv['house_pic_layout'] = $pic2;
            //图片处理并入库
            $pic_start=microtime();
            Log::$format = '[ Y-m-d H:i:s ]';

            $this->handleHezuHousePic($vv);
            Log::$format = '[ Y-m-d H:i:s ]';
            $pic_end=microtime();
            Log::write("【图片处理耗时】".($pic_end-$pic_start),'INFO');
        }
    }
    /*
     * 独立房源入库
     */
    public function aloneHouse($result,$value){

        Log::$format = '[ Y-m-d H:i:s ]';
        Log::write("【未对比成功】【gov_id为：】".$value['id'],'INFO');
        //权重 大于 0 但小于 35 把is_contrast = 5;
        $data['is_contrast'] = 5;
        //权重 等于 0 把is_contrast = 7;独立房源
        if(empty($result)){$data['is_contrast'] = 7;}
        $this->house_hezu_gov->where('id='.$value['id'])->save($data);
        Log::write("【修改gov表状态】".$this->house_hezu_gov->getLastSql(),'INFO');
        //获取房源自增id
        $new_num = $this->getAutoid();
        $value['gov_id'] = $value['id'];
        $value['id'] = $new_num;
        Log::write("【获取自增ID】".$new_num,'INFO');
        Log::write("【获取gov_id】".$value['gov_id'],'INFO');
        //做为一套新房源入库
        //插入到house_hezu表
        $this->house_hezu->data($value)->add();
        Log::write("插入house_hezu表【".$this->house_hezu->getLastSql()."】",'INFO');
        //插入到house_hezu_info表
        $this->house_hezu_info->data($value)->add();
        Log::write("插入house_hezu_info表【".$this->house_hezu_info->getLastSql()."】",'INFO');
        //计算house_hezu新增字段
        $this->houseCount($new_num);
        Log::$format = '[ Y-m-d H:i:s ]';
        Log::write("【计算house_hezu表中的三个字段】【house_hezu的id为：】".$new_num,'INFO');
        $pic1 = array();
        $pic1 = explode('|',$value['house_pic_unit']);
        $value['house_pic_unit'] = $pic1;
        //户型图
        $pic2 = array();
        $pic2 = explode('|',$value['house_pic_layout']);
        $value['house_pic_layout'] = $pic2;
        //图片处理并入库
        $pic_start=microtime();
        Log::$format = '[ Y-m-d H:i:s ]';
        $this->handleHezuHousePic($value);
        Log::$format = '[ Y-m-d H:i:s ]';
        $pic_end=microtime();
        Log::write("【图片处理耗时】".($pic_end-$pic_start),'INFO');
    }
	/*
	 *	用其他经纪公司数据   精确所在楼层    对比 house_hezu 表中的数据
	 *	大于35%，则更新house_hezu_info表中的数据
	 *	否则，作为一个单独的房源入库
	 * */
	public function fangzhuVsother(){
		$this->upyun = new UpYun('zhaijiso-pic', 'kys', 'zjsZJS123');
	    //提供房源自增id表
	    $auto = M('auto_increment_id');
	    //出售房源主表
	    $model=M('house_hezu');
	    //出售房源信息表
	    $model_info = M('house_hezu_info');
	    //房源图片表
	    $house_hezu_pic = M('house_hezu_pic');
	    //经纪公司官网表
	    $house_hezu_gov = M('house_hezu_gov');
	    
	    //查出gov表中 业主未对比的数据
	    $list = array();
		$start=microtime();
	    $list=$house_hezu_gov->where('is_contrast = 2 and source = 10')->limit("0,100")->select();
	    while(!empty($list)){
	        foreach($list as $key=>$value){
	            $value['created'] = microtime(true);
	            $value['updated'] = microtime(true);
	            //如果所在楼层or总楼层为 0 or cityarea_id = null 不参与对比把 gov 表中的is_contrast 该为 8
	            if($value['house_floor'] == "0" || $value['house_floor'] == "" || $value['house_topfloor'] == "0" ||$value['house_topfloor'] == "" || $value['cityarea_id'] == ''|| $value['house_room_totalarea']=='' ||$value['house_room_totalarea']==0){
	                $data=array();
	                $data['is_contrast'] = 8;
	                $house_hezu_gov->where('id='.$value['id'])->save($data);
	                continue;
	            }
	            $weight_tmp = 0;
	            $candidate=array();
	            $result=array();
	            //最低价格
	            $min_price = 100000000000;
	            // 最近时间
	            $last_time = 0;
	            //不同中介公司的房源数目
	            $cnt = 0;
	            $is_num = is_numeric($value['house_floor']);
	            if($is_num){
	                $house_floor = $value['house_floor'];
	                if($value['house_floor']<($value['house_topfloor']/3)){
	                    $value['house_floor'] = "低";
	                }elseif($value['house_floor']>=($value['house_topfloor']/3) && $value['house_floor']<=($value['house_topfloor']*(2/3))){
	                    $value['house_floor'] = "中";
	                }else{
	                    $value['house_floor'] = "高";
	                }
	                Log::write("【所在楼层是数字 已转换为 汉字】".$value['house_floor'],'INFO');
	                //用其他经纪公司的数据查 house_hezu_info 表
	                $result = $model->table(array('house_hezu'=>'a','house_hezu_info'=>'b'))
	                ->field('a.*,b.*')
	                ->where('a.id=b.id and b.house_topfloor='.$value['house_topfloor'].
	                    ' and( b.house_floor = '.$house_floor.
	                    ' or b.house_floor like "%'.$value['house_floor'].'%") and a.borough_id = '.$value['borough_id'].
	                    ' and a.house_room = '.$value['house_room'].
	                    ' and (
                                                        (a.house_room_totalarea >= '.$value['house_room_totalarea'].'-1'.
	                    ' and a.house_room_totalarea <='.$value['house_room_totalarea'].'+1
                                                        )
                                                        or a.house_room_totalarea ='.$value['house_room_totalarea'].'
                                                ) and b.source != '.$value['source']
	                )
	                ->group('b.id')
	                ->select();
	                Log::write("【用gov表数据做条件查 house_hezu_info 表】".$model->getLastSql(),'INFO');
	            }
	            //$v    info 表中的数据
	            //$value  gov 表中  source = 10  业主的数据
	            if($result){
	                //放置最大的中介公司的该套房源信息
	                $candidate = $result[0];
	                //寻找全局最大匹配度
	                foreach($result as $k=>$v){
	                        $weight=0;
	                        Log::write("house_hezu_info房源ID【".$v['id']."】与 其他经纪公司房源ID【".$value['id']."】对比开始",'INFO');
	                        //对比厅
	                        if($v['house_hall'] == $value['house_hall']){
	                            $weight +='31.00';
	                            Log::write("对比厅后，权重：【".$weight."】",'INFO');
	                        }
	                        //对比房屋价格 上下浮动5%
							
							if($value['house_price']*0.95<=$v['house_price'] &&  $v['house_price']<= $value['house_price']*1.05){
								$weight_price=(1-abs($value['house_price']-$v['house_price'])/$value['house_price']/0.05)*25.00;
								$weight_price=$weight_price==0?1:$weight_price;
								//容错
								$weight_price=$weight_price>25?25:$weight_price;
								$weight +=$weight_price;
								Log::write("对比价格后，权重：【".$weight."】",'INFO');
							}else{
								$weight_price=(1-abs($value['house_price']-$v['house_price'])/$value['house_price']/0.05)*60.00;
								$weight_price=$weight_price==0?1:$weight_price;
								$weight_price=$weight_price<-60?-60:$weight_price;
								$weight +=$weight_price;
								Log::write("对比价格后，权重：【".$weight."】",'INFO');
							}
							
							
	                        //对比装修情况
	                        if($v['house_fitment'] == $value['house_fitment']){
	                            $weight +='25.00';
	                            Log::write("对比装修后，权重：【".$weight."】",'INFO');
	                        }
	                        //对比朝向
	                        if(self::tVt($v['house_toward'],$value['house_toward'])){
	                            $weight +='12.00';
	                            Log::write("对比朝向后，权重：【".$weight."】",'INFO');
	                        }
	                        //对比卫生间
	                        if($v['house_toilet'] == $value['house_toilet']){
	                            $weight +='7.00';
	                            Log::write("对比卫生间后，权重：【".$weight."】",'INFO');
	                        }
	            
	                        Log::write("第".$k."套房源, 权重：".$weight, "INFO");
	                        //筛选最大的房源
	                        if($weight_tmp < $weight){
	                            $candidate = $v;
	                            $weight_tmp = $weight;
	                        }
	                        if($weight>=35){
	                            $cnt++;
	                        }
	                    
	                }
	                //如果全局最大匹配度大于35则对比匹配成功，入库
	                if($weight_tmp >= 35){
	                    Log::write("房源最大的权重：".$weight_tmp, "INFO");
	                    Log::write("总权重值:【".$weight_tmp."】",'INFO');
	                    //把对比成功的业主的数据 is_contrast 改为3对比成功
	                    $data['is_contrast'] = 3;
	                    if(!empty($house_floor)){
	                        $value['house_floor'] = $house_floor;
	                    }
	                    $house_hezu_gov->where('id='.$value['id'])->save($data);
	                    Log::write(":修改已对比过的数据状态【".M()->getLastSql()."】",'INFO');
	                    //把对比成功的业主的数据 插入到  house_hezu_info
	                    $value['gov_id'] = $value['id'];
	                    $value['id'] = $candidate['id'];
	                    $value['weight'] = $weight_tmp;
	                    $value['created'] = microtime(true);
	                    $value['updated'] = microtime(true);
	                    $model_info->data($value)->add();
	                    $this->houseCount($value['id']);
	                    
	                    Log::write("插入house_hezu_info表【".$model_info->getLastSql()."】",'INFO');
                        $pic = explode('|',$value['house_pic_unit']);
                        $value['house_pic_unit'] = $pic;
                        $pic_start=microtime();
                        Log::$format = '[ Y-m-d H:i:s ]';
                        
                        $this->handleHezuHousePic($value);
                        
                        Log::$format = '[ Y-m-d H:i:s ]';
                        $pic_end=microtime();
                        Log::write("【图片处理耗时】".($pic_end-$pic_start),'INFO');
                        
	                    $end=microtime();
	                    Log::$format = '[ Y-m-d H:i:s ]';
	                    Log::write("【  查找，对比，修改，插入 总耗时】".($end-$start),'INFO');
	                }else{//全局最大匹配度小于35，则作为一套独立房源入库
	                    //把未对比成功的其他经纪公司的数据 is_contrast 改为 5 模糊对比时  0<权重<35
	                    $arr_house=$value;
	                    $data['is_contrast'] = 5;
	                    if(!empty($house_floor)){
	                        $arr_house['house_floor'] = $house_floor;
	                    }
	                    $house_hezu_gov->where('id='.$value['id'])->save($data);
	                    Log::write("【修改gov表状态】".M()->getLastSql(),'INFO');
	                    //获取房源自增id
// 	                    $auto->where("name = 'house_hezu'")->setInc('auto_id','1');
// 	                    $new_num = $auto->where("name = 'house_hezu'")->getField('auto_id');
	                    $new_num = $this->getAutoid();
	                    $arr_house['id']=$new_num;
	                    $arr_house['gov_id']=$value['id'];
	                    $arr_house['cnt']=1;
	                    $arr_house['created'] = microtime(true);
	                    $arr_house['updated'] = microtime(true);
	                    $arr_house['house_desc'] = trimall($arr_house['house_desc']);
	                    $arr_house['weight'] = $weight_tmp;
	                    Log::write("【获取自增ID】".$new_num,'INFO');
	                    Log::write("【获取gov_id】".$value['gov_id'],'INFO');
	                    //做为一套新房源入库
	                    //插入到house_hezu表
	                    $model->data($arr_house)->add();
	                    Log::write("插入house_hezu表【".$model->getLastSql()."】",'INFO');
	                    //插入到house_hezu_info表
	                    $model_info->data($arr_house)->add();
	                    Log::write("插入house_hezu_info表【".$model_info->getLastSql()."】",'INFO');
	                    
	                    $this->houseCount($arr_house['id']);
	                    $pic = explode('|',$value['house_pic_unit']);
                        $arr_house['house_pic_unit'] = $pic;
                        $pic_start=microtime();
                        Log::$format = '[ Y-m-d H:i:s ]';
                        $this->handleHezuHousePic($arr_house);
                        Log::$format = '[ Y-m-d H:i:s ]';
                        $pic_end=microtime();
                        Log::write("【图片处理耗时】".($pic_end-$pic_start),'INFO');
	                }
	            }else{
	                //无可对比房源，则作为一套独立房源入库
	                //把未对比成功的业主的数据 is_contrast 改为 7 模糊对比时无可对比房源
	                $arr_house=$value;
                    $data['is_contrast'] = 7;
                    if(!empty($house_floor)){
                        $arr_house['house_floor'] = $house_floor;
                    }
                    $house_hezu_gov->where('id='.$value['id'])->save($data);
                    Log::write("【修改gov表状态】".M()->getLastSql(),'INFO');
                    //获取房源自增id
//                     $auto->where("name = 'house_hezu'")->setInc('auto_id','1');
//                     $new_num = $auto->where("name = 'house_hezu'")->getField('auto_id');
                    $new_num = $this->getAutoid();
                    $arr_house['id']=$new_num;
                    $arr_house['gov_id']=$value['id'];
                    $arr_house['cnt']=1;
                    $arr_house['created'] = microtime(true);
                    $arr_house['updated'] = microtime(true);
                    $arr_house['house_desc'] = trimall($arr_house['house_desc']);
                    Log::write("【获取自增ID】".$new_num,'INFO');
                    Log::write("【获取gov_id】".$value['gov_id'],'INFO');
                    //做为一套新房源入库
                    //插入到house_hezu表
                    $model->data($arr_house)->add();
                    Log::write("插入house_hezu表【".$model->getLastSql()."】",'INFO');
                    //插入到house_hezu_info表
                    $model_info->data($arr_house)->add();
                    Log::write("插入house_hezu_info表【".$model_info->getLastSql()."】",'INFO');
                    
                    $this->houseCount($arr_house['id']);
                    
                    $pic = explode('|',$value['house_pic_unit']);
                    $arr_house['house_pic_unit'] = $pic;
                    $pic_start=microtime();
                    Log::$format = '[ Y-m-d H:i:s ]';
                    
                    $this->handleHezuHousePic($arr_house);
                    
                    Log::$format = '[ Y-m-d H:i:s ]';
                    $pic_end=microtime();
                    Log::write("【图片处理耗时】".($pic_end-$pic_start),'INFO');
	            }
	        }
	        usleep(5000);
	        $list=$house_hezu_gov->where('is_contrast = 2 and is_fill=2 and source = 10')->limit("0,100")->select();
	    }
	}
	
	
	
	
	
	/*
	 * 朝向对比时只要有一个方向相同就认为朝向相同
	 * */
	public function tVt($t1,$t2){
	    if($t1=='1'){
	        if($t2 == '1' || $t2 == '5' || $t2 == '7' || $t2 == '10' || $t2 == '11' || $t2 == '12' || $t2 == '14' || $t2 == '15'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='2'){
	        if($t2 == '2' || $t2 == '6' || $t2 == '8' || $t2 == '10' || $t2 == '11' || $t2 == '13' || $t2 == '14' || $t2 == '15'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='3'){
	        if($t2 == '3' || $t2 == '5' || $t2 == '6' || $t2 == '9' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '15'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='4'){
	        if($t2 == '4' || $t2 == '7' || $t2 == '8' || $t2 == '9' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='5'){
	        if($t2 == '5' || $t2 == '1' || $t2 == '3' || $t2 == '6' || $t2 == '7' || $t2 == '9' || $t2 == '10'  || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='6'){
	        if($t2 == '6' || $t2 == '2' || $t2 == '3' || $t2 == '5' || $t2 == '8' || $t2 == '9' || $t2 == '10' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='7'){
	        if($t2 == '7' || $t2 == '1' || $t2 == '4' || $t2 == '5' || $t2 == '8' || $t2 == '9' || $t2 == '10' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='8'){
	        if($t2 == '8' || $t2 == '2' || $t2 == '4' || $t2 == '6' || $t2 == '7' || $t2 == '9' || $t2 == '10' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='9'){
	        if($t2 == '9' || $t2 == '3' || $t2 == '4' || $t2 == '5' || $t2 == '6' || $t2 == '7' || $t2 == '8' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='10'){
	        if($t2 == '10' || $t2 == '1' || $t2 == '2' || $t2 == '5' || $t2 == '6' || $t2 == '7' || $t2 == '8' || $t2 == '11' || $t2 == '12' || $t2 == '13' || $t2 == '14'  || $t2 == '15'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='11'){
	        if($t2 !== '4'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='12'){
	        if($t2 !== '2'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='13'){
	        if($t2 !== '1'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='14'){
	        if($t2 !== '3'){
	            return true;
	        }else{
	            return false;
	        }
	    }
	    if($t1=='15'){
	        return true;
	    }
	}
	
/**
 * 房源图片保存(house_Hezu_info表使用)
 */
function handleHezuHousePic($info){
	//房源图片表
	$house_hezu_pic = M('house_hezu_pic');
	$pic_info = array();
	if($info){
		foreach($info as $k=>$v){
			//房间图
			
			$pic_info = array();
			$tmpPicSelect=array();
			
			if($k == 'house_pic_unit'){
			        foreach($v as $value){
			            if(empty($value)){
			                continue;
			            }else{
			                //数据库中是否存在，如果存在获取id，不存在插入图片地址表中；
			                $tmpPicSelect=array(
			                    'pic_source_url'=>$value,
			                );
			                $picId = $house_hezu_pic->where($tmpPicSelect)->find();
			                if(!$picId){
			                	$fhunit = file_get_contents($value);
    							$time = microtime(true)*10000;
    							$ypy_path = '/hezu/unit/'.$time.'.jpg';
    							$rsp1 = $this->upyun->writeFile($ypy_path, $fhunit, true);   // 上传图片，自动创建目录
    							$endTime = microtime(true);
								$pic_path = 'http://zhaijiso-pic.b0.upaiyun.com'.$ypy_path; 
			                	
			                    $pic_info[]=array(
			                        'pic_source_url'=>$value,
			                        'pic_url'=>$pic_path,
			                        'house_hezu_id'=>$info['id'],
			                        'gov_id'=>$info['gov_id'],
			                        'pic_type'=>1,
			                        'time'=>date('Y-m-d h:i:s',time())
			                    );
			                }
			                //						$info[$i]['house_pic_layout'] = empty($info[$i]['house_pic_layout'])?$pic_unit_id:$info[$i]['house_pic_layout'].",".$pic_unit_id;
			            }
			            
			        }
			        $house_hezu_pic->addAll($pic_info);
			}
			//户型图
			$pic_info = array();
			$tmpPicSelect=array();
			if($k == "house_pic_layout"){
			        foreach($v as $value){
			            if(empty($value)){
			                continue;
			            }else{
			                //数据库中是否存在，如果存在获取id，不存在插入图片地址表中；
			                $tmpPicSelect=array(
			                    'pic_source_url'=>$value,
			                );
			                 
			                $picId = $house_hezu_pic->where($tmpPicSelect)->find();
			                if(!$picId){
			                	$fhunit = file_get_contents($value);
    							$time = microtime(true)*10000;
    							$ypy_path = '/hezu/layout/'.$time.'.jpg';
    							$rsp1 = $this->upyun->writeFile($ypy_path, $fhunit, true);   // 上传图片，自动创建目录
    							$endTime = microtime(true);
								$pic_path = 'http://zhaijiso-pic.b0.upaiyun.com'.$ypy_path; 
			                    $pic_info[]=array(
			                        'pic_source_url'=>$value,
			                        'pic_url'=>$pic_path,
			                        'house_hezu_id'=>$info['id'],
			                        'gov_id'=>$info['gov_id'],
			                        'pic_type'=>2,
			                        'time'=>date('Y-m-d h:i:s',time())
			                    );
			                }
			            }
			        }
			        $house_hezu_pic->addAll($pic_info);
			}

		}
	}
}
/*
 * 全量计算house_hezu表中cnt,minprice,is_fangzhu,updated
 *  */

function handleHouse() {
    // 出售房源主表
    $model_hezu = M ( 'house_hezu' );
    // 出售房源info表
    $model_info = M ( 'house_hezu_info' );
    // 经纪公司官网表
    $model_gov = M ( 'house_hezu_gov' );
    $model_pic = M ( 'house_hezu_pic' );

    $model_info_delete = M ( 'house_hezu_info_delete' );
    // gov数据
    $result_insert = array ();
    $count_num = $model_hezu->count ();

    for($intNum = 0; $intNum <= $count_num/100; $intNum ++) {
        $left = $intNum * 100;
        $house_list = $model_hezu->limit ( "$left,100" )->select ();
        $count = 0;
        if(!empty($house_list)){
            foreach ( $house_list as $key => $value ) {
                $this->houseCount($value['id']);
            }
        }
    }
}

//获取自增id
public function getAutoid() {
    $autoid = new Model();
    $ret = $autoid->execute('update auto_increment_id set auto_id = last_insert_id(auto_id+1) where id = 4');
    if ($ret === false) {
        return false;
    } else {
        return mysql_insert_id();
    }
}

}

















